package cn.ant.config;


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/***
 * RabbitMQ配置（交换机、队列、以及绑定关系）
 * @author Anhui AntLaddie <a href="https://juejin.cn/user/4092805620185316">(掘金蚂蚁小哥)</a>
 * @version 1.0
 **/
@Configuration
public class RabbitMQConfig {

    // 普通直接交换机
    public static final String ORDINARY_DIRECT_EXCHANGE = "ordinaryDirectExchange";
    // 普通队列
    public static final String ORDINARY_QUEUE = "ordinaryQueue";
    // 路由key
    public static final String ROUTE_KEY = "routeKey";

    /***
     * 创建一个普通的直接交换机
     * @return Exchange
     */
    @Bean("ordinaryDirectExchange")
    public Exchange createOrdinaryDirectExchange() {
        return ExchangeBuilder.directExchange(ORDINARY_DIRECT_EXCHANGE).durable(true).build();
    }

    /***
     * 创建一个普通队列
     * @return Queue
     */
    @Bean("ordinaryQueue")
    public Queue createOrdinaryQueue() {
        return QueueBuilder.durable(ORDINARY_QUEUE).build();
    }

    /***
     * 绑定关系
     * @param exchange 交换机
     * @param queue 队列
     * @return Binding
     */
    @Bean("ordinaryQueueBindOrdinaryDirectExchange")
    public Binding createOrdinaryQueueBindExchange(
            @Qualifier("ordinaryDirectExchange") Exchange exchange,
            @Qualifier("ordinaryQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTE_KEY).noargs();
    }
}
